package com.maven.front.controller.frontTeacher;

import com.maven.front.bean.EduCourse;
import com.maven.front.bean.EduTeacher;
import com.maven.front.bean.Result;
import com.maven.front.bean.SysSubject;
import com.maven.front.service.frontTeacher.TeacharService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user/teacher")
public class TeacherController {
    @Autowired
    private TeacharService teacharService;

    //查询所有课程分类
    @GetMapping("selSubject")
    public Result selectSubject(){
        List<SysSubject> list=teacharService.selectSubject();
        if(list==null){
            return  new Result(400,"查询失败",null);
        }
        else {
            return  new Result(400,"查询成功",list);
        }
    }

    //查询所有的教师列表
    @GetMapping("getList")
    public  Result getList(){
        List<EduTeacher> list=teacharService.getList();
        if(list==null){
            return  new Result(400,"查询失败",null);
        }
        else {
            return  new Result(200,"查询成功",list);
        }
    }

    //根据课程id查询教师列表
    @PostMapping("getById")
    public  Result getById(EduTeacher eduTeacher){
        List<EduTeacher> teacher=teacharService.getById(eduTeacher);
        if(teacher==null){
            return  new Result(400,"查询失败",null);
        }
        else {
            return  new Result(200,"查询成功",teacher);
        }
    }
    //根据id获取教师详细信息
    @PostMapping("selectKey")
    public  Result selectKey(int id){
        EduTeacher teacher=teacharService.selectKey(id);
        if(teacher==null){
            return  new Result(400,"查询失败",null);
        }
        else {
            return  new Result(200,"查询成功",teacher);
        }
    }
    //根据SubjectId 获取CourseName
    @PostMapping("getSubjectId")
    public  Result getSubjectId(int subjectId){
        List<EduCourse> list=teacharService.getSubjectId(subjectId);
        for( EduCourse l:list){
            System.out.println(l.getCourseName()+"**********************"+l.getCourseId()+"************"+l.getLogo());
        }
        if(list==null){
            return  new Result(400,"查询失败",null);
        }
        else {
            return  new Result(200,"查询成功",list);
        }
    }

    @GetMapping("getByIdAndSubjectId")
    public Result getTeacher(Integer id){
        EduTeacher teacher=teacharService.selectKey(id);
        List<EduCourse> list=teacharService.getSubjectId(teacher.getSubjectId());
        Map<String ,Object> map = new HashMap<>();
        map.put("courses",list);
        map.put("teacher",teacher);
        if(ObjectUtils.isEmpty(teacher)){
            return  new Result(400,"查询失败",null);
        }
        return new Result(200,"查询成功",map);
    }
}
